
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Promise · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="teaching.html" />
    
    
    <link rel="prev" href="print-talbe.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    前端进阶
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Grid System.html">
            
                <a href="Grid System.html">
            
                    
                    Grid System
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="Handwritten signature.html">
            
                <a href="Handwritten signature.html">
            
                    
                    Handwritten Signature
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Mobile Soft Keyboard.html">
            
                <a href="Mobile Soft Keyboard.html">
            
                    
                    Mobile Soft Keyboard
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="authentication.html">
            
                <a href="authentication.html">
            
                    
                    Authentication
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Code
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" >
            
                <span>
            
                    
                    Four Operations
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="control loading.html">
            
                <a href="control loading.html">
            
                    
                    Control Loading
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="downfile.html">
            
                <a href="downfile.html">
            
                    
                    Downfile
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="dynamic-routing.html">
            
                <a href="dynamic-routing.html">
            
                    
                    Dynamic Routing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="ecs.html">
            
                <a href="ecs.html">
            
                    
                    Ecs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="engineering.html">
            
                <a href="engineering.html">
            
                    
                    Engineering
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="eslint-vscode-format.html">
            
                <a href="eslint-vscode-format.html">
            
                    
                    Eslint Vscode Format
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="eventloop.html">
            
                <a href="eventloop.html">
            
                    
                    Eventloop
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="four-operations-1.html">
            
                <a href="four-operations-1.html">
            
                    
                    Four Operations 1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="four-operations-2.html">
            
                <a href="four-operations-2.html">
            
                    
                    Four Operations 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="four-operations-3.html">
            
                <a href="four-operations-3.html">
            
                    
                    Four Operations 3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="four-operations-4.html">
            
                <a href="four-operations-4.html">
            
                    
                    Four Operations 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="front end summary.html">
            
                <a href="front end summary.html">
            
                    
                    Front End Summary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="git commit style.html">
            
                <a href="git commit style.html">
            
                    
                    Git Commit Style
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="git-commands.html">
            
                <a href="git-commands.html">
            
                    
                    Git Commands
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="high-quality-code.html">
            
                <a href="high-quality-code.html">
            
                    
                    High Quality Code
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="history.html">
            
                <a href="history.html">
            
                    
                    History
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="html-tag.html">
            
                <a href="html-tag.html">
            
                    
                    Html Tag
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="http-https-http2.html">
            
                <a href="http-https-http2.html">
            
                    
                    Http Https Http2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="ignore log.html">
            
                <a href="ignore log.html">
            
                    
                    Ignore Log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" >
            
                <span>
            
                    
                    Imgs
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="javascript-optimization.html">
            
                <a href="javascript-optimization.html">
            
                    
                    Javascript Optimization
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="local application.html">
            
                <a href="local application.html">
            
                    
                    Local Application
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="locality.html">
            
                <a href="locality.html">
            
                    
                    Locality
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="loop-unrolling.html">
            
                <a href="loop-unrolling.html">
            
                    
                    Loop Unrolling
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="mobile recommendation.html">
            
                <a href="mobile recommendation.html">
            
                    
                    Mobile Recommendation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="modal.html">
            
                <a href="modal.html">
            
                    
                    Modal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="monitor.html">
            
                <a href="monitor.html">
            
                    
                    Monitor
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="performance.html">
            
                <a href="performance.html">
            
                    
                    Performance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="print-talbe.html">
            
                <a href="print-talbe.html">
            
                    
                    Print Talbe
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.37" data-path="promise.html">
            
                <a href="promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="teaching.html">
            
                <a href="teaching.html">
            
                    
                    Teaching
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.39" data-path="vue refresh.html">
            
                <a href="vue refresh.html">
            
                    
                    Vue Refresh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.40" data-path="vue-page-not-refresh.html">
            
                <a href="vue-page-not-refresh.html">
            
                    
                    Vue Page Not Refresh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.41" data-path="why-learn-theory.html">
            
                <a href="why-learn-theory.html">
            
                    
                    Why Learn Theory
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Promise</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x624B;&#x5199;-promise">&#x624B;&#x5199; Promise</h1>
<p>&#x5B8C;&#x6574;&#x6E90;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Promise</span> </span>{
    <span class="hljs-keyword">constructor</span>(executor) {
        <span class="hljs-keyword">this</span>.onFulfilleds = []
        <span class="hljs-keyword">this</span>.onRejecteds = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&apos;pending&apos;</span>

        <span class="hljs-keyword">const</span> self = <span class="hljs-keyword">this</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resolve</span>(<span class="hljs-params">value</span>) </span>{
            <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;pending&apos;</span>) {
                self.state = <span class="hljs-string">&apos;fulfilled&apos;</span>
                self.value = value
                self.onFulfilleds.forEach(callback =&gt; callback())
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reject</span>(<span class="hljs-params">reason</span>) </span>{
            <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;pending&apos;</span>) {
                self.state = <span class="hljs-string">&apos;rejected&apos;</span>
                self.reason = reason
                self.onRejecteds.forEach(callback =&gt; callback())
            }
        }

        <span class="hljs-keyword">try</span> {
            executor(resolve, reject)
        } <span class="hljs-keyword">catch</span> (error) {
            reject(error)
        }
    }

    then(onFulfilled, onRejected) {
        onFulfilled = <span class="hljs-keyword">typeof</span> onFulfilled == <span class="hljs-string">&apos;function&apos;</span>? onFulfilled : value =&gt; value
        onRejected = <span class="hljs-keyword">typeof</span> onRejected == <span class="hljs-string">&apos;function&apos;</span>? onRejected : reason =&gt; { <span class="hljs-keyword">throw</span> reason }

        <span class="hljs-keyword">const</span> self = <span class="hljs-keyword">this</span>
        <span class="hljs-keyword">const</span> promise2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
            <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;fulfilled&apos;</span>) { <span class="hljs-comment">// &#x9488;&#x5BF9;&#x7ACB;&#x5373; resolve(): new Promise(r =&gt; r())</span>
                setTimeout(() =&gt; {
                    <span class="hljs-keyword">try</span> {
                        <span class="hljs-keyword">const</span> x = onFulfilled(self.value)
                        resolvePromise(promise2, x, resolve, reject)
                    } <span class="hljs-keyword">catch</span> (error) {
                        reject(error)
                    }
                })
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;rejected&apos;</span>) { <span class="hljs-comment">// &#x9488;&#x5BF9;&#x7ACB;&#x5373; reject(): new Promise((r, j) =&gt; j())</span>
                setTimeout(() =&gt; {
                    <span class="hljs-keyword">try</span> {
                        <span class="hljs-keyword">const</span> x = onRejected(self.reason)
                        resolvePromise(promise2, x, resolve, reject)
                    } <span class="hljs-keyword">catch</span> (error) {
                        reject(error)
                    }
                })
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;pending&apos;</span>) { <span class="hljs-comment">// &#x9488;&#x5BF9;&#x5F02;&#x6B65; resolve reject: new Promise((r, j) =&gt; setTimeout(() =&gt; r()))</span>
                self.onFulfilleds.push(() =&gt; {
                    setTimeout(() =&gt; {
                        <span class="hljs-keyword">try</span> {
                            <span class="hljs-keyword">const</span> x = onFulfilled(self.value)
                            resolvePromise(promise2, x, resolve, reject)
                            <span class="hljs-comment">// resolvePromise &#x4EE3;&#x7801;&#x4F5C;&#x7528;&#x548C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5DEE;&#x4E0D;&#x591A;</span>
                            <span class="hljs-comment">// if (x instanceof Promise) {</span>
                            <span class="hljs-comment">//     x.then(val =&gt; resolve(val))</span>
                            <span class="hljs-comment">// } else {</span>
                            <span class="hljs-comment">//     resolve(x)</span>
                            <span class="hljs-comment">// }</span>
                        } <span class="hljs-keyword">catch</span> (error) {
                            reject(error)
                        }
                    })
                })

                self.onRejecteds.push(() =&gt; {
                    setTimeout(() =&gt; {
                        <span class="hljs-keyword">try</span> {
                            <span class="hljs-keyword">const</span> x = onRejected(self.reason)
                            resolvePromise(promise2, x, resolve, reject)
                        } <span class="hljs-keyword">catch</span> (error) {
                            reject(error)
                        }
                    })
                })
            }
        })

        <span class="hljs-keyword">return</span> promise2
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resolvePromise</span>(<span class="hljs-params">promise2, x, resolve, reject</span>) </span>{
    <span class="hljs-keyword">if</span> (promise2 == x) reject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span> (<span class="hljs-string">&apos;circular reference&apos;</span>))

    <span class="hljs-keyword">if</span> (x !== <span class="hljs-literal">null</span> &amp;&amp; <span class="hljs-keyword">typeof</span> x == <span class="hljs-string">&apos;object&apos;</span> || <span class="hljs-keyword">typeof</span> x == <span class="hljs-string">&apos;function&apos;</span>) {
        <span class="hljs-keyword">let</span> called = <span class="hljs-literal">false</span>
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">let</span> then = x.then
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> then === <span class="hljs-string">&apos;function&apos;</span>) {
                then.call(x, (y) =&gt; {
                    <span class="hljs-keyword">if</span> (called) <span class="hljs-keyword">return</span>
                    called = <span class="hljs-literal">true</span>
                    resolvePromise(promise2, y, resolve, reject)
                }, (r) =&gt; {
                    <span class="hljs-keyword">if</span> (called) <span class="hljs-keyword">return</span>
                    called = <span class="hljs-literal">true</span>
                    reject(r)
                })
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">if</span> (called) <span class="hljs-keyword">return</span>
                called = <span class="hljs-literal">true</span>
                resolve(x)
            }
        } <span class="hljs-keyword">catch</span> (e) {
            <span class="hljs-keyword">if</span> (called) <span class="hljs-keyword">return</span>
            called = <span class="hljs-literal">true</span>
            reject(e)
        }
    } <span class="hljs-keyword">else</span> {
        resolve(x)
    }
}

<span class="hljs-built_in">Promise</span>.defer = <span class="hljs-built_in">Promise</span>.deferred = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> dfd = {}
    dfd.promise = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        dfd.resolve = resolve
        dfd.reject = reject
    })

    <span class="hljs-keyword">return</span> dfd
}

<span class="hljs-built_in">module</span>.exports = <span class="hljs-built_in">Promise</span>
</code></pre>
<p>&#x6BCF;&#x4E2A; <code>then()</code> &#x65B9;&#x6CD5;&#x90FD;&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684; Promise&#xFF0C;&#x800C;&#x8FD9;&#x4E2A; Promise &#x7684;&#x72B6;&#x6001;&#x662F; <code>fulfilled</code> &#x8FD8;&#x662F; <code>rejected</code> &#x53D6;&#x51B3;&#x4E8E;&#x4E0A;&#x4E00;&#x4E2A; Promise &#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((r, j) =&gt; {
    setTimeout(() =&gt; {
        r(<span class="hljs-string">&apos;p0 resolve&apos;</span>)
    }, <span class="hljs-number">2000</span>)
})

p
.then(val =&gt; {
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;p0 callback&apos;</span>
})
.then(val =&gt; {
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;p1 callback&apos;</span>
})
.then(val =&gt; {
   <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;p2 callback&apos;</span>
})
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x8FF0;&#x793A;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;p0 callback &#x51B3;&#x5B9A;&#x4E86; p1 callback &#x7684;&#x72B6;&#x6001;&#x3002;
&#x5982;&#x679C; p0 callback &#x8FD4;&#x56DE;&#x7684;&#x662F; Promise&#xFF0C;&#x5219; p1 callback &#x9700;&#x6C42;&#x7B49;&#x5F85;&#x8FD9;&#x4E2A; Promise resolve() &#x4E4B;&#x540E;&#x624D;&#x4F1A;&#x6267;&#x884C;&#x3002;
&#x5982;&#x679C;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;&#x6570;&#x503C;&#xFF0C; p1 callback &#x5219;&#x4F1A;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5B8C;&#x6574;&#x7248;&#x7684;&#x6E90;&#x7801;&#x4E0D;&#x5BB9;&#x6613;&#x770B;&#x61C2;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x4E00;&#x4E0B;&#x7B80;&#x5316;&#x7248;&#x7684;&#xFF0C;&#x5C11;&#x4E86;&#x5F88;&#x591A;&#x5197;&#x4F59;&#x5224;&#x65AD;&#xFF0C;&#x53EA;&#x4FDD;&#x7559;&#x4E86;&#x6267;&#x884C;&#x7684;&#x4E3B;&#x7EBF;&#x7A0B;&#xFF08;&#x8FC7;&#x4E0D;&#x4E86;&#x6D4B;&#x8BD5;&#xFF09;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Promise</span> </span>{
    <span class="hljs-keyword">constructor</span>(executor) {
        <span class="hljs-keyword">this</span>.onFulfilleds = []
        <span class="hljs-keyword">this</span>.onRejecteds = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&apos;pending&apos;</span>

        <span class="hljs-keyword">const</span> self = <span class="hljs-keyword">this</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resolve</span>(<span class="hljs-params">value</span>) </span>{
            <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;pending&apos;</span>) {
                self.state = <span class="hljs-string">&apos;fulfilled&apos;</span>
                self.value = value
                self.onFulfilleds.forEach(callback =&gt; callback())
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reject</span>(<span class="hljs-params">reason</span>) </span>{
            <span class="hljs-keyword">if</span> (self.state == <span class="hljs-string">&apos;pending&apos;</span>) {
                self.state = <span class="hljs-string">&apos;rejected&apos;</span>
                self.reason = reason
                self.onRejecteds.forEach(callback =&gt; callback())
            }
        }

        <span class="hljs-keyword">try</span> {
            executor(resolve, reject)
        } <span class="hljs-keyword">catch</span> (error) {
            reject(error)
        }
    }

    then(onFulfilled, onRejected) {
        onFulfilled = <span class="hljs-keyword">typeof</span> onFulfilled == <span class="hljs-string">&apos;function&apos;</span> ? onFulfilled : value =&gt; value
        onRejected = <span class="hljs-keyword">typeof</span> onRejected == <span class="hljs-string">&apos;function&apos;</span> ? onRejected : reason =&gt; { <span class="hljs-keyword">throw</span> reason }

        <span class="hljs-keyword">const</span> self = <span class="hljs-keyword">this</span>
        <span class="hljs-keyword">const</span> promise2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
            self.onFulfilleds.push(() =&gt; {
                setTimeout(() =&gt; {
                    <span class="hljs-keyword">try</span> {
                        <span class="hljs-keyword">const</span> x = onFulfilled(self.value)
                        <span class="hljs-keyword">if</span> (x <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Promise</span>) {
                            x.then(val =&gt; resolve(val))
                        } <span class="hljs-keyword">else</span> {
                            resolve(x)
                        }
                    } <span class="hljs-keyword">catch</span>(error) {
                        reject(error)
                    }
                })
            })

            self.onRejecteds.push(() =&gt; {
                setTimeout(() =&gt; {
                    <span class="hljs-keyword">try</span> {
                        <span class="hljs-keyword">const</span> x = onRejected(self.reason)
                        <span class="hljs-keyword">if</span> (x <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Promise</span>) {
                            x.then(val =&gt; resolve(val))
                        } <span class="hljs-keyword">else</span> {
                            resolve(x)
                        }
                    } <span class="hljs-keyword">catch</span>(error) {
                        reject(error)
                    }
                })
            })
        })

        <span class="hljs-keyword">return</span> promise2
    }
}
</code></pre>
<h2 id="&#x624B;&#x5199;-promise-&#x5176;&#x4ED6;&#x65B9;&#x6CD5;">&#x624B;&#x5199; Promise &#x5176;&#x4ED6;&#x65B9;&#x6CD5;</h2>
<pre><code class="lang-js"><span class="hljs-built_in">Promise</span>.resolve = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(r =&gt; {
        r(value)
    })
}

<span class="hljs-built_in">Promise</span>.reject = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((r, j) =&gt; {
        j(value)
    })
}

<span class="hljs-built_in">Promise</span>.prototype.catch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">errCallback</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.then(<span class="hljs-literal">null</span>, errCallback)
}

<span class="hljs-comment">// val &#x4E0D;&#x4F1A;&#x4F20;&#x7ED9; finally &#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14; finally &#x8FD8;&#x9700;&#x8981;&#x5C06; val &#x4F20;&#x9012;&#x7ED9;&#x540E;&#x9762;&#x7684; then&#x3002;</span>
<span class="hljs-built_in">Promise</span>.prototype.finally = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.then((val) =&gt; {
        callback()
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(r =&gt; r(val))
    }, (err) =&gt; {
        callback()
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(r =&gt; r(err))
    })
}

<span class="hljs-built_in">Promise</span>.all = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">promises</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">Array</span>.isArray(promises)) {
            <span class="hljs-keyword">return</span> reject(<span class="hljs-string">&apos;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x4E3A;&#x6570;&#x7EC4;&apos;</span>)
        }

        <span class="hljs-keyword">if</span> (!promises.length) <span class="hljs-keyword">return</span>

        <span class="hljs-keyword">const</span> result = []
        <span class="hljs-keyword">const</span> len = promises.length
        <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>
        promises.forEach(p =&gt; {
            <span class="hljs-built_in">Promise</span>.resolve(p)
                .then(val =&gt; {
                    count++
                    result.push(val)
                    <span class="hljs-keyword">if</span> (count == len) {
                        resolve(result)
                    }
                })
                .catch(e =&gt; reject(e))
        })
    })
}

<span class="hljs-built_in">Promise</span>.race = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">promises</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">Array</span>.isArray(promises)) {
            <span class="hljs-keyword">return</span> reject(<span class="hljs-string">&apos;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x4E3A;&#x6570;&#x7EC4;&apos;</span>)
        }

        <span class="hljs-keyword">if</span> (!promises.length) <span class="hljs-keyword">return</span>

        promises.forEach((p, i) =&gt; {
            <span class="hljs-built_in">Promise</span>.resolve(p)
                .then(val =&gt; resolve(val))
                .catch(e =&gt; reject(e))
        })
    })
}
</code></pre>
<h2 id="&#x53C2;&#x8003;&#x8D44;&#x6599;">&#x53C2;&#x8003;&#x8D44;&#x6599;</h2>
<ul>
<li><a href="https://github.com/YvetteLau/Blog/issues/2" target="_blank">Promise&#x7684;&#x6E90;&#x7801;&#x5B9E;&#x73B0;&#xFF08;&#x5B8C;&#x7F8E;&#x7B26;&#x5408;Promise/A+&#x89C4;&#x8303;&#xFF09;</a></li>
<li><a href="https://juejin.im/post/5dc383bdf265da4d2d1f6b23" target="_blank">&#x53EF;&#x80FD;&#x662F;&#x76EE;&#x524D;&#x6700;&#x6613;&#x7406;&#x89E3;&#x7684;&#x624B;&#x5199;promise</a></li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="print-talbe.html" class="navigation navigation-prev " aria-label="Previous page: Print Talbe">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="teaching.html" class="navigation navigation-next " aria-label="Next page: Teaching">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Promise","level":"1.37","depth":1,"next":{"title":"Teaching","level":"1.38","depth":1,"path":"前端进阶/teaching.md","ref":"前端进阶/teaching.md","articles":[]},"previous":{"title":"Print Talbe","level":"1.36","depth":1,"path":"前端进阶/print-talbe.md","ref":"前端进阶/print-talbe.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["summary","livereload"],"pluginsConfig":{"summary":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"前端进阶/promise.md","mtime":"2021-02-15T12:04:36.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-24T08:50:55.666Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

